package com.yl.chainStore.payrule.order.entity

import com.yl.chainStore.common.Constants
import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.payrule.common.RuleField
import com.yl.chainStore.payrule.order.OrderRule
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * 满送
 */
data class OrderFullMoneySendMoneyRule(

	var buyMoney:Long,

	var disCountMoney:Long
):OrderRule{

	override fun validationSelf(): ResponseBody {
		if(disCountMoney > buyMoney){
			return returnError(ErrorCode.OPERATION_DATA_ERROR,"折扣金额大于订单金额")
		}
		return returnSuccess()
	}

	override fun canUseForOrder(order: PayCustomerOrder): Boolean {
		return order.originalPrice > buyMoney
	}

	/**
	 * 订单金额
	 */
	@RuleField(viewName = "订单金额")
	var payMoneyShow: BigDecimal?=null
		get() {
			if (field == null) {
				return BigDecimal(buyMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if (value != null) {
				buyMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	/**
	 * 折扣金额
	 */
	@RuleField(viewName = "折扣金额")
	var disCountMoneyShow: BigDecimal?=null
		get() {
			if (field == null) {
				return BigDecimal(disCountMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if (value != null) {
				disCountMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

}
